/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import {Package} from '@carbon/icons-react';
import OptionButton from './OptionButton';
import useDownloadVideo from './useDownloadVideo';
import {useState} from 'react';
import {sessionAtom} from '@/demo/atoms';
import {useSetAtom} from 'jotai';

export default function DownloadOption() {
  // const {download,startTask, state} = useDownloadVideo();
  const {startTask} = useDownloadVideo();
  const [isButtonDisabled, setButtonDisabled] = useState(false);
  const [title, setTitle] = useState('开始执行任务');
  const [state, setState] = useState('default');
  const setSession = useSetAtom(sessionAtom);
  const [tips, setTips] = useState('');

  function start(params: any) {
    setState('started');
    startTask()
      .then(res => {
        console.log(res);
        if (res.code === 0) {
          setButtonDisabled(true);
          setTitle('任务已提交');
          setTips(res.message);
          setSession(previousSession =>
            previousSession == null
              ? previousSession
              : {...previousSession, saved: true},
          );
        } else {
          console.error('任务执行失败，状态码:', res.code);
        }
        setState('completed');
      })
      .catch(err => {
        setState('error');
        console.log(err);
      });
  }

  // return (
  //   <OptionButton
  //     title="Download"
  //     Icon={Package}
  //     loadingProps={{
  //       loading: state === 'started' || state === 'encoding',
  //       label: 'Downloading...',
  //     }}
  //     onClick={download}
  //   />
  // );
  return (
    <div>
      <OptionButton
        title={title}
        Icon={Package}
        loadingProps={{
          loading: state === 'started',
          label: '正在执行...',
        }}
        onClick={isButtonDisabled ? () => {} : start}
        disabled={isButtonDisabled}
      />
      {tips?<p style={{textAlign:'center',marginTop:'5px'}}>{tips}</p>:''}
    </div>
  );
}
